<template>
  <div class="login-container">
    <div class="login-inner box-left">
      <!-- <div class="login-left">
        <p class="title">Welcome</p>
        <p>欢迎登录{{ defaultSettingsTitle }}</p>
      </div> -->
      <div class="login-right">
        <el-form
          ref="loginForm"
          :model="loginForm"
          class="login-form"
          autocomplete="on"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">欢迎登录</h3>
            <div class="titsystem">智慧校园学生卡管理平台</div>
          </div>
          <p class="item-small-title">输入账号</p>
          <el-form-item prop="username">
            <span class="">
              <!-- <svg-icon icon-class="user" /> -->
              <img class="user" src="@/assets/login/account.png" alt="">
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>
          <p class="item-small-titlet">输入密码</p>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="spanpassword">
                <!-- <svg-icon icon-class="password" /> -->
                <img class="password" src="@/assets/login/password.png" alt="">
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd svg-container" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>

          <el-button
            :loading="loading"
            type="primary"
            @click="handleLogin"
          >登 录</el-button>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import defaultSettings from '@/settings'
export default {
  name: 'Login',
  components: {},
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      defaultSettingsTitle: defaultSettings.title,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      userid: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
              // this.$message({
              //   type: 'success',
              //   message: '登录成功'
              // })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 0px 5px 12px 15px;
      color: #333;
      height: 47px;

      &:-webkit-autofill {
        -webkit-text-fill-color: #333 !important;
        transition: background-color 5000s ease-in-out 0s;
      }
    }
  }

  .el-form-item {
    border-bottom: 1.5px solid #a9a9a9;
    color: #333333;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  height: 100%;
  width: 100%;
  min-width: 1065px;
  background: url(~@/assets/login/login_bg.jpg) no-repeat;
  background-size: 100% 100%;
  // background-size: auto 100%;
  overflow: hidden;

  .login-inner {
    // box-shadow: 0px 2px 19px 0px rgba(53, 99, 171, 0.3);
    // background: #fff;
    background: url(~@/assets/login/login_bgm.png);
    background-size: 100% 100%;
    max-width: 100%;
    max-height: 100%;
    margin: 154px auto 0;
    width: 900px;
    min-width: 900px;
    height: 500px;
    border-radius: 5px;
    .login-left {
      // background: url(~@/assets/login/login_left.png);
      // width: 485px;
      height: 613px;
      position: absolute;
      left: -5px;
      top: -7px;
      .title {
        font-size: 40px;
        font-family: Adobe Heiti Std;
        font-weight: normal;
        color: rgba(255, 255, 255, 1);
        line-height: 40px;
        padding-top: 280px;
        text-align: center;
        margin: 0;
      }
      p {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(255, 255, 255, 1);
        line-height: 40px;
        text-align: center;
      }
    }
    .login-right {
      // background: #fff;
      padding-left: 100px;
      width: 431px;
      height: 500px;
      position: absolute;
      // overflow: hidden;
      // display: flex;
      // align-items: center;
      // margin-left: 140px;
    }
  }
  // .login-form {
  //   position: relative;
  //   max-width: 100%;
  //   margin: 250px auto 0;
  //   overflow: hidden;
  //   width: 1065px;
  //   height: 598px;
  //   background: rgba(255, 255, 255, 1);
  //   box-shadow: 0px 2px 19px 0px rgba(53, 99, 171, 0.3);
  //   border-radius: 5px;
  // }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .item-small-title {
    margin: 0;
    padding: 0;
    font-size: 18px;
    color:#514177;
    line-height: 40px;
    font-weight: 400;
    padding-top: 10px;
  }
  .item-small-titlet{
    margin: 0;
    padding: 0;
    font-size: 18px;
    color:#514177;
    line-height: 40px;
    font-weight: 400;

  }
  .svg-container {
    margin: -3px 5px 6px 0px;
    color: #a9a9a9;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .user{
    width: 14px;
    height: 15px;
  }
  .spanpassword{
    padding-top: 3px;
    .password{
    width: 16px;
    height: 16px;
  }
  }
  .title-container {
    position: relative;
    padding-top:55px;
    // padding: 90px 0 55px 0;

    .title {
      color: #333333;
      margin: 0px auto 0 auto;
      font-weight: bold;
      font-size: 30px;
      font-weight: 600;
      line-height: 40px;
      font-family: PingFang SC;
    }
    .titsystem{
      font-size:20px;
      font-weight:600;
      font-family: PingFang SC;
      color:rgba(51,51,51,1);
      line-height:45px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
.login-container  .el-button{
  width:100%;
  height:40px;
  box-shadow:0px 5px 10px 0px rgba(58,122,247,0.3);
  border-radius:8px 0px 8px 8px;
  margin-top: 20px;
  // padding: 0;
}
.login-container .el-button--primary{
  background:linear-gradient(90deg,rgba(95,125,247,1),rgba(76,145,247,1));
}
</style>
